﻿using System;
using System.Collections.Generic;
using System.Text;
using ebay.Dal.CenterDB_BasTableAdapters;
using ebay.Dal.CenterDB_LTableAdapters;
using ebay.Utility;
using ebay.Dal;
using System.Data.SqlClient;
using System.Data;

namespace ebay.Dal
{
    public class ItemChangePriceHis
    {
        //修改和下架本地数据存储
        public bool Insert(string ebayUserId, string ItemId, string ProductId, double Orignal_Price, double Modify_Price,double Fee,string Muser,string Mnote)
        {
            DateTime date = DateTime.Now;

            string sqltext = @"insert into ItemChangePriceHis(ItemId,EbayUserId,ProductId,Orignal_Price,Modify_Price,Fee,Muser,Mtime,MNote)
	                                values (@ItemId,@EbayUserId,@ProductId,@Orignal_Price,@Modify_Price,@Fee,@Muser,@Mtime,@Mnote)";
            string sqltext2 = @"Update ProductTracking set ProcurementStatus=@ProcurementStatus,Recommendations=@Recommendations,IsOutOfStock=@isoutofStock,Muser =@Muser,Mtime =GETDATE() where ProductId =@ProductId";
            SqlParameter[] paras ={
                                      new SqlParameter ("@ItemId",SqlDbType.VarChar),
                                      new SqlParameter ("@EbayUserId",SqlDbType.VarChar),
                                      new SqlParameter ("@ProductId",SqlDbType.VarChar),
                                      new SqlParameter ("@Orignal_Price",SqlDbType.Decimal),
                                      new SqlParameter ("@Modify_Price",SqlDbType.Decimal),
                                      new SqlParameter ("@Fee",SqlDbType.Decimal),
                                      new SqlParameter ("@Muser",SqlDbType.VarChar),
                                      new SqlParameter ("@Mtime",SqlDbType.DateTime),
                                      new SqlParameter ("@Mnote",SqlDbType.VarChar),
                                  };
            paras[0].Value = ItemId;
            paras[1].Value = ebayUserId;
            paras[2].Value = ProductId;
            paras[3].Value = Orignal_Price;
            paras[4].Value = Modify_Price;
            paras[5].Value = Fee;
            paras[6].Value = Muser;
            paras[7].Value = date;
            paras[8].Value = Mnote;

            int status = 0;//客服建议
            int isoutofStock = 0;//销售状态
            int procureStatus = 1;//采购状态
            if (Mnote == "修改价格")
            {
                status = 1;
                isoutofStock = 4006;
                procureStatus = 1;
            }
            if (Mnote == "商品下架")
            {
                status = 2;
                isoutofStock = 4007;
                procureStatus = 3;
            }

            SqlParameter[] p = {
                                   new SqlParameter("@ProcurementStatus", SqlDbType.Int),
                                   new SqlParameter("@Recommendations", SqlDbType.Int),
                                   new SqlParameter("@isoutofStock", SqlDbType.Int),
                                   new SqlParameter("@Muser", SqlDbType.VarChar),                                  
                                   new SqlParameter("@ProductId", SqlDbType.VarChar)
                               };
            p[0].Value = procureStatus;
            p[1].Value = status;
            p[2].Value = isoutofStock;
            p[3].Value = Muser;
            p[4].Value = ProductId;
            using (SqlConnection connection = new SqlConnection(SqlHelper.ceterDB_Conn))
            {
                connection.Open();
                using (SqlTransaction trans = connection.BeginTransaction())
                {
                    int val = 0;
                    try
                    {
                        try
                        {
                            val += SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sqltext, paras);
                            val += SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sqltext2, p);
                            trans.Commit();
                        }
                        catch
                        {
                            trans.Rollback();
                        }
                        if (val > 0)
                        {
                            return true;
                        }
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
            return false;
        }
    }
}
